package com.lovo.order.controller;

import com.alibaba.fastjson2.JSONObject;
import com.lovo.order.dao.IOrderDao;
import com.lovo.order.dao.ITicketDao;
import com.lovo.order.dao.ITicketIndexDao;
import com.lovo.order.entity.TicketEntity;
import com.lovo.order.entity.TicketEntity_Index;
import com.lovo.order.service.IRemoteMovieService;
import com.lovo.order.vo.TicketVo;
import com.lovo.util.ResultTokenVo;
import com.lovo.util.SysUtil;
import com.lovo.util.Verifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.http.HttpRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@RequestMapping("order")
@RestController
public class OrderController {
    @Autowired
    private IOrderDao orderDao;
    @Autowired
    private ITicketDao ticketDao;
    @Autowired
    private ITicketIndexDao ticketIndexDao;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @GetMapping("pages/{start}/{end}/{currentPage}/{pageSize}")
    public Page<Map>  pages(@PathVariable("start")float start,
                            @PathVariable("end")float end,
                            @PathVariable("currentPage")int currentPage,
                            @PathVariable("pageSize")int pageSize,
                            HttpServletRequest request
    ){
       String userName=String.valueOf(request.getAttribute("userName"));
        System.out.println(userName);
            PageRequest pageRequest=PageRequest.of(currentPage-1,pageSize);
            return     orderDao.pagesOrder(start,end,userName,pageRequest);


    }
    @GetMapping("ticketEntityList")
    public List<TicketEntity> ticketEntityList(){

     return (List<TicketEntity>) ticketDao.findAll();
    }
   @PostMapping("addCar")
    public boolean addCar(@RequestBody TicketVo vo,HttpServletRequest request){
   String userName= String.valueOf(request.getAttribute("userName"));
   vo.setUserName(userName);
   //判断商品是否存在
       if(redisTemplate.opsForHash().hasKey(userName,vo.getMovieName())){
      LinkedHashMap map=     (LinkedHashMap)redisTemplate.opsForHash()
                   .get(userName,vo.getMovieName());
       int num=Integer.parseInt(String.valueOf(map.get("num"))) ;
       vo.setNum(num+1);

       }else {
           vo.setNum(1);
       }
       redisTemplate.opsForHash()
               .put(userName, vo.getMovieName(), vo);
       redisTemplate.expire(vo.getMovieName(), 30, TimeUnit.DAYS);
  return true;

 }
 @GetMapping("ticketCarList/{userName}")
 public List<Object> ticketCarList(@PathVariable("userName")String userName){

 return   redisTemplate.opsForHash().values(userName);
 }

 @PutMapping("updateCar")
 public boolean updateCar(@RequestBody TicketVo vo){
  String userName= vo.getUserName();
  String movieName=vo.getMovieName();
  //放入新对象
     redisTemplate.opsForHash().put(userName,movieName,vo);
     return true;
 }
@DeleteMapping("delCar/{userName}/{movieName}")
 public boolean delCar(@PathVariable("userName")String userName,
                       @PathVariable("movieName")String movieName){
   redisTemplate.opsForHash().delete(userName,movieName);
    return true;
 }

@GetMapping("ticketEntity_indices/{info}")
 public List<TicketEntity_Index> ticketEntity_indices(@PathVariable("info")String info){
 return ticketIndexDao.findAllByInfo(info);
 }

 @GetMapping("ticketEntityId/{id}")
 public TicketEntity ticketEntityId(@PathVariable("id") long id){
     Optional<TicketEntity> optional=ticketDao.findById(id);
     if(optional.isPresent()){
         return optional.get();
     }
     return null;
 }
 @GetMapping("notifyMessage")
 public boolean notifyMessage(){
        Object obj=
     redisTemplate.opsForList()
             .leftPop(SysUtil.NOTIFY_KEY,5,TimeUnit.SECONDS);
     if(null!=obj){
         return  true;
     }
     return false;
    }


}
